﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// SampleMeterCorrectionControl.xaml 的交互逻辑
    /// </summary>
    public partial class SampleMeterCorrectionControl : UserControl
    {
        public SampleMeterCorrectionControl()
        {
            InitializeComponent();
        }


        //修正点
        static DependencyProperty CorrectionPointProperty = DependencyProperty.Register(
            "CorrectionPoint",
            typeof(string),
            typeof(SampleMeterCorrectionControl)
            );

        public string CorrectionPoint
        {
            set { SetValue(CorrectionPointProperty,value); }
            get { return (string)GetValue(CorrectionPointProperty); }
        }


        //修正值
        static DependencyProperty CorrectionValueProperty = DependencyProperty.Register(
            "CorrectionValue",
            typeof(string),
            typeof(SampleMeterCorrectionControl)
            );

        public string CorrectionValue
        {
            set { SetValue(CorrectionValueProperty, value); }
            get { return (string)GetValue(CorrectionValueProperty); }
        }


        //添加按钮被点击
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            Thickness thisThickness = Margin;
            double thisHeight = 30;
            Grid parentGrid = (Grid)Parent;
            #region 在当前控件下面添加一个控件
            SampleMeterCorrectionControl correctionControl = new SampleMeterCorrectionControl();
            int sonCount = parentGrid.Children.Count;
            parentGrid.Children.Add(correctionControl);
            sonCount = parentGrid.Children.Count;
            correctionControl.HorizontalAlignment = HorizontalAlignment.Center;
            correctionControl.VerticalAlignment = VerticalAlignment.Top;
            double left = thisThickness.Left;
            double top = thisThickness.Top + thisHeight + 5;
            double right = thisThickness.Right;
            double bottom = thisThickness.Bottom;
            //linearControl.Margin = new Thickness(left,top,right,bottom);
            //linearControl.Height = thisHeight;
            #endregion

            #region 移动当前控件下面的控件
            foreach (UIElement element in parentGrid.Children)
            {
                SampleMeterCorrectionControl control = element as SampleMeterCorrectionControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if (controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop + thisHeight + 5, controlRight, controlBottom);
                }
            }
            #endregion
            correctionControl.Margin = new Thickness(left, top, right, bottom);
            correctionControl.Height = thisHeight;
        }

        //添加控件
        public void AddControl()
        {
            AddButton_Click(AddButton, new RoutedEventArgs());
        }


        //删除按钮被点击
        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            Thickness thisThickness = Margin;
            Grid parentGrid = (Grid)Parent;
            double top = thisThickness.Top;
            double thisHeight = Height;
            if (parentGrid.Children.Count < 2)
            {
                return;
            }
            //删除控件
            parentGrid.Children.Remove(this);
            #region 移动当前控件下面的控件
            foreach (UIElement element in parentGrid.Children)
            {
                SampleMeterCorrectionControl control = element as SampleMeterCorrectionControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if (controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop - thisHeight - 5, controlRight, controlBottom);
                }
            }
            #endregion
        }

        //文本框内容发生变化时，如果内容不能转为double，显示红色
        private void TextValueChanged(object sender, TextChangedEventArgs args)
        {
            TextBox senderBox = sender as TextBox;
            string textValue = senderBox.Text.Trim();
            try
            {
                Convert.ToDouble(textValue);
                senderBox.Foreground = Brushes.Black;
            }
            catch
            {
                senderBox.Foreground = Brushes.Red;
            }
        }
    }
}
